package cn.chrisx.drad.admob;

import android.content.Context;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;

import cn.chrisx.drad.core.DrAdError;
import cn.chrisx.drad.core.DrAdPlatform;
import cn.chrisx.drad.platform.BasePlatformAd;

/**
 * @author Jzfox <jz_boy@163.com>
 * @version 1.0
 * @date 2018/9/26 9:28
 * @description
 * @copyright ©2018 www.jzfox.net All rights reserved.
 */
abstract class BaseAd extends BasePlatformAd {


    protected AdRequest adRequest = new AdRequest.Builder().build();

    protected AdListener googleAdListener = new AdListener() {

        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
            onAdLoadSuccess();
        }

        @Override
        public void onAdClicked() {
            BaseAd.super.onAdClicked();
        }

        @Override
        public void onAdFailedToLoad(int err) {
            // Code to be executed when an ad request fails.

            String errMsg;


            switch (err) {
                case AdRequest.ERROR_CODE_INTERNAL_ERROR:
                    errMsg = "内部发生了一些事情; 例如，广告服务器收到了无效的回复。";
                    break;
                case AdRequest.ERROR_CODE_INVALID_REQUEST:
                    errMsg = "广告请求无效; 例如，广告单元ID不正确。";
                    break;
                case AdRequest.ERROR_CODE_NETWORK_ERROR:
                    errMsg = "由于网络连接，广告请求失败。";
                    break;
                case AdRequest.ERROR_CODE_NO_FILL:
                    errMsg = "广告请求已成功完成，但由于缺少广告资源，没有返回广告。";
                    break;
                default:
                    errMsg = "unknown";
                    break;
            }

            onAdLoadFailed(DrAdError.ERROR_THIRD, new DrAdError(AdMobPlatform.instance().name(), err, errMsg));
        }

        @Override
        public void onAdOpened() {
            // Code to be executed when an ad opens an overlay that
            // covers the screen.
            onAdShow();

        }

        @Override
        public void onAdLeftApplication() {
            onAdDismiss();
        }

        @Override
        public void onAdClosed() {

        }

    };

    @Override
    public boolean loadAd(Context context) {
        MobileAds.initialize(context, drAdConfig.getAppId());
        return false;
    }

    @Override
    public void onDestroy(Context context) {

    }


    @Override
    public DrAdPlatform platform() {
        return AdMobPlatform.instance();
    }
}
